package com.jd.dl.web;

import java.util.List;

import ognl.Ognl;

import org.apache.log4j.Logger;

import freemarker.ext.beans.StringModel;
import freemarker.template.SimpleHash;
import freemarker.template.TemplateMethodModelEx;
import freemarker.template.TemplateModelException;

@SuppressWarnings("rawtypes")
public final class BeanFieldEval implements TemplateMethodModelEx {
	private Logger log = Logger.getLogger(BeanFieldEval.class);

	@SuppressWarnings("unchecked")
	public Object exec(List args) throws TemplateModelException {
		try {
			String fieldName =  args.get(0).toString();

			Object basedata = null;
			if (args.get(1) instanceof StringModel) {
				basedata = ((StringModel) args.get(1)).getWrappedObject();
			} else {
				basedata = ((SimpleHash) args.get(1)).toMap();
			}

			return Ognl.getValue(fieldName, basedata);

		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}

		return "";
	}
}
